package org.dlsu.mt.parser.dictionary;

/**
 * made by LEFT thesis group
 *
 * monolingual dictionary module
 *
 * WordClass class
 * represents as the structure for a word class
 * consists of the part of speech it is associated with, the semantic information, and the collocation
 */

public class WordClass
{
	protected final String id; //id for the word class, to be used in connection with the transfer dictionary
	protected String POS; //contains the part of speech of the word class
	protected List semantics; //contains the list of semantic information contained in the word class
	protected CollocationList collocation; //contains the list of collocations associated with the word class
	protected List fs;
	protected List lfg;
	protected int nCtr = 0, nFSCtr = 0, nLFGCtr = 0;
	
	public WordClass(String id, String POS)
	{
		/**
		 * constructor with parameter containing the part of speech of the word class
		 * semantic and collocation lists are initialized as empty lists
		 */
		 
		this.id = id;
		this.POS = POS;
		semantics = new List();
		fs = new List();
		collocation = new CollocationList();
		nCtr = 0;
		nFSCtr = 0;
		nLFGCtr = 0;
	}
	
	public WordClass(String id, String POS, List semantics, CollocationList collocation)
	{
		/**
		 * constructor with parameters of instantiating the object with the part of speech, semantics, and collocation
		 */
		 
		this.id = id;
		this.POS = POS;
		this.semantics = semantics;
		this.collocation = collocation;
		nCtr = semantics.getListCount();
		nFSCtr = 0;
	}
	
	public WordClass(String id, String POS, List semantics, List fs, List lfg)
	{
		/**
		 * constructor with parameters of instantiating the object with the part of speech, semantics, and collocation
		 */
		 
		this.id = id;
		this.POS = POS;
		this.semantics = semantics;
		this.fs = fs;
		this.lfg = lfg;
		nCtr = semantics.getListCount();
		nFSCtr = fs.getListCount();
		nLFGCtr = lfg.getListCount();
	}
	
	public WordClass(String id, String POS, List semantics, List fs)
	{
		/**
		 * constructor with parameters of instantiating the object with the part of speech, semantics, and collocation
		 */
		 
		this.id = id;
		this.POS = POS;
		this.semantics = semantics;
		this.fs = fs;
		nCtr = semantics.getListCount();
		nFSCtr = fs.getListCount();
	}
	
	public WordClass(String id, String POS, List fs)
	{
		/**
		 * constructor with parameters of instantiating the object with the part of speech, semantics, and collocation
		 */
		 
		this.id = id;
		this.POS = POS;
		this.fs = fs;
		nCtr = 0;
		nFSCtr = fs.getListCount();
	}
	
	public String getPOS()
	{
		/**
		 * returns the part of speech of the word class
		 */
		return POS;
	}
	
	public int getCtr()
	{
		return nCtr;
	}
	
	public int getFSCtr()
	{
		return nFSCtr;
	}
	
	public int getLFGCtr()
	{
		return nLFGCtr;
	}
	
	public String getNextLFG()
	{
		/**
		 * gets the next element in the semantic list, activates the iterator if it is inactive, resets it to the
		 * beginning of the iteration if it has reached the end of the list
		 */
		 
		if(lfg.iterationActive())
		{
			if(!lfg.hasMoreElements())
			{
				lfg.resetIterator();
			}
			return lfg.nextElement();
		}
		else
		{
			lfg.resetIterator();
			return lfg.nextElement();
		}
	}
	
	public String getNextSemantics()
	{
		/**
		 * gets the next element in the semantic list, activates the iterator if it is inactive, resets it to the
		 * beginning of the iteration if it has reached the end of the list
		 */
		 
		if(semantics.iterationActive())
		{
			if(!semantics.hasMoreElements())
			{
				semantics.resetIterator();
			}
			return semantics.nextElement();
		}
		else
		{
			semantics.resetIterator();
			return semantics.nextElement();
		}
	}
	
	public Collocation getNextCollocation()
	{
		/**
		 * gets the next element in the collocation list, activates the iterator if it is inactive, resets it to the
		 * beginning of the iteration if it has reached the end of the list
		 */
		 
		if(collocation.iterationActive())
		{
			if(!collocation.hasMoreElements())
			{
				collocation.resetIterator();
			}
			return collocation.nextElement();
		}
		else
		{
			collocation.resetIterator();
			return collocation.nextElement();
		}
	}
	
	public String getNextFS()
	{
		/**
		 * gets the next element in the collocation list, activates the iterator if it is inactive, resets it to the
		 * beginning of the iteration if it has reached the end of the list
		 */
		 
		if(fs.iterationActive())
		{
			if(!fs.hasMoreElements())
			{
				fs.resetIterator();
			}
			return fs.nextElement();
		}
		else
		{
			fs.resetIterator();
			return fs.nextElement();
		}
	}
	
	public boolean addSemantics(String entry)
	{
		/**
		 * adds a semantic information to the semantic list
		 * returns true if add is successful
		 * returns false otherwise
		 */
		 
		return semantics.addElement(entry);
	}
	
	public boolean addCollocation(String value, String pos)
	{
		/**
		 * adds a collocation to the collocation list by specifying the value and part of speech of the collocated
		 * wordkl
		 *
		 * returns true if add is successful
		 * returns false otherwise
		 */
		 
		return collocation.addElement(value, pos);
	}
	
	public boolean addCollocation(Collocation c)
	{
		/**
		 * adds a collocation to the collocation list by passing a collocation object
		 * returns true if add is successful
		 * returns false otherwise
		 */
		 
		return collocation.addElement(c);
	}
	
	public boolean removeSemantics(String entry)
	{
		/**
		 * removes a semantic information from the list of semantics
		 * returns true if remove is successful
		 * returns false otherwise
		 */
		 
		return semantics.removeElement(entry);
	}
	
	public boolean removeCollocation(String entry)
	{
		/**
		 * removes an associated collocation from the list of collocations
		 * returns true if remove is successful
		 * returns false otherwise
		 */
		 
		return collocation.removeElement(entry);
	}
	
	public void mergeWordClass(WordClass w)
	{
		/**
		 * merges the word class passed in the parameter with this word class
		 * copies all semantic information and collocation
		 */
		 
		if(w.getPOS().equals(POS))
		{
			String first = w.getNextSemantics();
			String temp = null;
			if(first!=null)
			{
				do
				{
					if(temp == null)
						temp = first;
					else
					{
						addSemantics(temp);
						temp = w.getNextSemantics();
					}
				}while(!temp.equals(first));
			}
			
			Collocation cfirst = w.getNextCollocation();
			Collocation ctemp = null;
			if(cfirst!=null)
			{
				do
				{
					if(ctemp == null)
						ctemp = cfirst;
					else
					{
						addCollocation(ctemp);
						ctemp = w.getNextCollocation();
					}
				}while(!ctemp.equals(cfirst));
			}
		}
	}
	
	public String getId()
	{
		return id;
	}
	
}